<?php

namespace App\Http\Controllers\Backend;

use App\Http\Requests\StorePostRequest;
use App\Http\Requests\UpdatePostRequest;
use App\Libraries\Markdown;
use App\Models\Post;
use App\Models\Tag;
use Illuminate\Http\Request;

class PostsController extends Controller
{
    /**
     * 文章列表
     *
     * @return \Illuminate\View\View
     */
    public function index(Request $requset)
    {
        $query = $requset->get('query');
        $posts = Post::search($query)->paginate();

        return view('backend.posts.index', compact('query', 'posts'));
    }

    /**
     * 文章添加页面
     *
     * @return \Illuminate\View\View
     */
    public function create()
    {
        $tags = Tag::get();
        $old_tags = old('tags', []);

        if (count($old_tags) > 0) {
            foreach ($old_tags as $tag) {
                if (! $tags->contains('name', $tag)) {
                    $tags = $tags->push(new Tag(['name' => $tag]));
                }
            }
        }

        return view('backend.posts.create', compact('tags', 'old_tags'));
    }

    /**
     * 文章添加
     *
     * @param  \App\Http\Requests\StorePostRequest $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(StorePostRequest $request)
    {
        $markdown = new Markdown();
        $markdown->convertMarkdownToHtml($request->get('body'));
        $markdown->generateToc();
        $tags = $request->get('tags', []);

        Post::disableSearchSyncing();

        $post = Post::create([
            'title'         => $request->get('title'),
            'slug'          => $request->get('slug'),
            'publish_date'  => $request->get('publish_date'),
            'body_original' => $request->get('body'),
            'body'          => $markdown->getHtml(),
            'toc'           => $markdown->getToc(),
        ]);

        Post::enableSearchSyncing();

        foreach ($tags as &$tag) {
            $tag = Tag::firstOrCreate(['name' => $tag])->id;
        }

        $post->tags()->sync($tags);
        $post->searchable();

        return redirect()->route('posts.index');
    }

    /**
     * 文章编辑页面
     *
     * @param  integer $id
     * @return \Illuminate\View\View
     */
    public function edit($id)
    {
        $post = Post::with('tags')->findOrFail($id);
        $tags = Tag::get();
        $old_tags = old('tags', []);

        if (count($old_tags) > 0) {
            foreach ($old_tags as $tag) {
                if (! $tags->contains('name', $tag)) {
                    $tags = $tags->push(new Tag(['name' => $tag]));
                }
            }
        } else {
            $old_tags = $post->tags->pluck('name')->toArray();
        }

        return view('backend.posts.edit', compact('post', 'tags', 'old_tags'));
    }

    /**
     * 文章编辑
     *
     * @param  integer $id
     * @param  \App\Http\Requests\UpdatePostRequest $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update($id, UpdatePostRequest $request)
    {
        $post = Post::findOrFail($id);
        $markdown = new Markdown();
        $markdown->convertMarkdownToHtml($request->get('body'));
        $markdown->generateToc();
        $tags = $request->get('tags', []);

        Post::disableSearchSyncing();

        $post->update([
            'title'         => $request->get('title'),
            'slug'          => $request->get('slug'),
            'publish_date'  => $request->get('publish_date'),
            'body_original' => $request->get('body'),
            'body'          => $markdown->getHtml(),
            'toc'           => $markdown->getToc(),
        ]);

        Post::enableSearchSyncing();

        foreach ($tags as &$tag) {
            $tag = Tag::firstOrCreate(['name' => $tag])->id;
        }

        $post->tags()->sync($tags);
        $post->searchable();

        return redirect()->route('posts.index');
    }

    /**
     * 文章删除
     *
     * @param  integer $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function destroy($id)
    {
        Post::destroy($id);

        return redirect()->route('posts.index');
    }
}
